package com.gljx.web.controller.JournalController;

import com.gljx.web.Entity.*;
import com.gljx.web.controller.CoaceGroupController;
import com.gljx.web.controller.PersonController;
import com.gljx.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/")
public class CarFuelJournalController {

	@Autowired
	private DepartmentService departmentService;
	
	@Autowired
	private CarService carService;
	
	@Autowired
	private PersonController PersonController;
	
	@Autowired
	private CoaceGroupController coaceGroupController;
	
	@Autowired
	private TeamService teamService;
	
	@Autowired
	private CarRefuelEntryService carRefuelEntryService;
	
	@Autowired
	private OtherCarAddOilEntryService otherCarAddOilEntryService;
	
	/**
	 * 车辆加油报表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/selCarFuelJournal")
	public String selCarFuelJournal(HttpServletRequest request) throws Exception{
		HttpSession session=request.getSession();
		Calendar cale = Calendar.getInstance();// 得到一个Calendar的实例
		 		 cale.add(Calendar.MONTH, 0);
		 		 cale.set(Calendar.DAY_OF_MONTH, 1);
		Date resultDate = cale.getTime(); // 结果
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String beginTime = sdf.format(resultDate);
		String endTime = sdf.format(new Date());
		User user=(User) session.getAttribute("user");
        List<Team> teams = teamService.queryTeam(user.getFcompanyid());
        Map map = PersonController.getPageAsJl(request, "", "", "0");
 		//查询教练组信息
        Map map2 = coaceGroupController.getPageCoaceGroup(request, "", "", "0");
        List<Car> cars = carService.queryCar(user.getFcompanyid());
        List<Department> departments = departmentService.selectAdminListAsfcontid("",user.getFcompanyid());
        Map<String , String> map3 = new HashMap<>();
        					 map3.put("beginTime", beginTime);
        					 map3.put("endTime", endTime);
        					 map3.put("companyId", user.getFcompanyid());
        List<CarRefuelEntry> carRefuelEntries = carRefuelEntryService.selectCarRefuelEntryForJournal(map3);
        List<OtherCarAddOilEntry> otherCarAddOilEntries = otherCarAddOilEntryService.selOtherCarAddOilForJournal(map3); 
        request.setAttribute("otherCarAddOilEntries", otherCarAddOilEntries);
        request.setAttribute("carRefuelEntries", carRefuelEntries);
		request.setAttribute("persons", map.get("sList"));
		request.setAttribute("pb", map.get("pb"));
		request.setAttribute("cars", cars);
		request.setAttribute("team", teams);
		request.setAttribute("beginTime", beginTime);
		request.setAttribute("endTime", endTime);
		request.setAttribute("pb2", map2.get("pb"));
        request.setAttribute("coacegroups", map2.get("sList"));
		request.setAttribute("departmentList",departments);
		return "backHome/journaling/carFuelJournaling/carFuelJournal";	
	}
	
	
	/**
	 * 车辆加油报表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/selCarFuelJournalByConOne")
	public String selCarFuelJournalByConOne(HttpServletRequest request,CarRefuelEntry carRefuelEntry,String beginTime,String  endTime) throws Exception{
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");
        List<Team> teams = teamService.queryTeam(user.getFcompanyid());
        Map map = PersonController.getPageAsJl(request, "", "", "0");
 		//查询教练组信息
        Map map2 = coaceGroupController.getPageCoaceGroup(request, "", "", "0");
        List<Car> cars = carService.queryCar(user.getFcompanyid());
        List<Department> departments = departmentService.selectAdminList("");
        Map<String , String> map3 = new HashMap<>();
        					 map3.put("beginTime", beginTime);
        					 map3.put("endTime", endTime);
        					 map3.put("team", carRefuelEntry.getCfteamcoach());
        					 map3.put("companyId", user.getFcompanyid());
        					 map3.put("coach", carRefuelEntry.getCftrainid());
        					 map3.put("coachGroup", carRefuelEntry.getCfgroupid());
        					 map3.put("deparment", (String) carRefuelEntry.getCfdepname());
        					 map3.put("car", carRefuelEntry.getCfcarnoid());			
        List<CarRefuelEntry> carRefuelEntries = carRefuelEntryService.selectCarRefuelEntryForJournal(map3);
        List<OtherCarAddOilEntry> otherCarAddOilEntries = otherCarAddOilEntryService.selOtherCarAddOilForJournal(map3);     
        request.setAttribute("otherCarAddOilEntries", otherCarAddOilEntries);
        request.setAttribute("carRefuelEntries", carRefuelEntries);
		request.setAttribute("persons", map.get("sList"));	
		request.setAttribute("pb", map.get("pb"));
		request.setAttribute("cars", cars);
		request.setAttribute("team", teams);
		request.setAttribute("beginTime", beginTime);
		request.setAttribute("endTime", endTime);
		request.setAttribute("carRefuelEntry", carRefuelEntry);
		request.setAttribute("pb2", map2.get("pb"));
        request.setAttribute("coacegroups", map2.get("sList"));
		request.setAttribute("departmentList",departments);
		return "backHome/journaling/carFuelJournaling/carFuelJournal";	
	}
	
	
	 
	
}
